package com.bkgy.modular.tbdsjrawcheckc622.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.datacollect_bx.bean.JDataSave622;
import com.bkgy.modular.qms.entity.QmsCurveDataMongo;
import com.bkgy.modular.tbdsjrawcheckc622.dao.TbDsjRawcheckC622Dao;
import com.bkgy.modular.tbdsjrawcheckc622.entity.ExportTbDsjRawcheckC622;
import com.bkgy.modular.tbdsjrawcheckc622.entity.JTbDsjRawcheckC622;
import com.bkgy.modular.tbdsjrawcheckc622.entity.TbDsjRawcheckC622;
import com.bkgy.modular.tbdsjrawcheckc622.service.TbDsjRawcheckC622Service;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.CodiUtil;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2130酸轧原料审核表接口具体实现类
 *
 * @author 生成器生成
 * @date 2022-10-18 11:59:23
 */
@Service
public class TbDsjRawcheckC622ServiceImpl implements TbDsjRawcheckC622Service {

    @Resource
    private TbDsjRawcheckC622Dao tbDsjRawcheckC622Dao;
    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 添加记录
     *
     * @param data   json字符串
     * @param userId 用户id
     * @param sname  用户姓名
     */
    public ResultData insertDataTbDsjRawcheckC622(String data, Long userId, String sname) {
        try {
            JTbDsjRawcheckC622 jtbDsjRawcheckC622 = JSON.parseObject(data, JTbDsjRawcheckC622.class);
            TbDsjRawcheckC622 tbDsjRawcheckC622 = jtbDsjRawcheckC622.getTbDsjRawcheckC622();
            CodiUtil.newRecord(userId, sname, tbDsjRawcheckC622);
            tbDsjRawcheckC622Dao.insertDataTbDsjRawcheckC622(tbDsjRawcheckC622);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null);
        }
    }

    ;

    /**
     * 根据主键删除对象
     *
     * @param indocno 对象主键
     */
    public ResultData deleteDataTbDsjRawcheckC622One(Long indocno) {
        try {
            tbDsjRawcheckC622Dao.deleteDataTbDsjRawcheckC622One(indocno);
            return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    }

    ;

    /**
     * 根据主键字符串删除多个对象
     *
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTbDsjRawcheckC622Many(String str_id) {
        try {
            String sql = "delete tb_dsj_rawcheck_c622 where indocno in(" + str_id + ")";
            tbDsjRawcheckC622Dao.deleteDataTbDsjRawcheckC622Many(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 修改记录
     *
     * @param userId 用户id
     * @param sname  用户姓名
     * @param data   sysUser 对象实体
     */
    public ResultData updateDataTbDsjRawcheckC622(String data, Long userId, String sname) {
        try {
            JTbDsjRawcheckC622 jtbDsjRawcheckC622 = JSON.parseObject(data, JTbDsjRawcheckC622.class);
            TbDsjRawcheckC622 tbDsjRawcheckC622 = jtbDsjRawcheckC622.getTbDsjRawcheckC622();
            CodiUtil.editRecord(userId, sname, tbDsjRawcheckC622);
            tbDsjRawcheckC622Dao.updateDataTbDsjRawcheckC622(tbDsjRawcheckC622);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null);
        }
    }

    ;

    /**
     * 分页查看记录
     *
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjRawcheckC622ByPage(String data) {
        try {
            JTbDsjRawcheckC622 jtbDsjRawcheckC622 = JSON.parseObject(data, JTbDsjRawcheckC622.class);
            JSONObject jsonObject = null;
            Integer pageIndex = jtbDsjRawcheckC622.getPageIndex();
            Integer pageSize = jtbDsjRawcheckC622.getPageSize();

            if (null == pageIndex || null == pageSize) {
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }

            if (null != jtbDsjRawcheckC622.getCondition()) {
                jsonObject = JSON.parseObject(jtbDsjRawcheckC622.getCondition().toString());
            }
            String hot_coil_id = null;//热卷号
            if (!StringUtils.isEmpty(jsonObject.get("hot_coil_id"))) {
                hot_coil_id = jsonObject.get("hot_coil_id").toString();
            }
            String steelgrade = null;//钢牌号
            if (!StringUtils.isEmpty(jsonObject.get("steelgrade"))) {
                steelgrade = jsonObject.get("steelgrade").toString();
            }
            String fin_grade = null;//等级
            if (!StringUtils.isEmpty(jsonObject.get("fin_grade"))) {
                fin_grade = jsonObject.get("fin_grade").toString();
            }


            String dbegin = null; //开始时间
            if (StringUtils.hasText(jsonObject.getString("dbegin"))) {
                dbegin = jsonObject.getString("dbegin");
            }

            String dend = null; //结束时间
            if (StringUtils.hasText(jsonObject.getString("dend"))) {
                dend = jsonObject.getString("dend");
            }

            List<Integer> steelgroup = new ArrayList<>(); // 钢种
            if (jsonObject.getJSONArray("steelgroup") != null) {
                steelgroup = jsonObject.getJSONArray("steelgroup");
            }

            List<TbDsjRawcheckC622> list = tbDsjRawcheckC622Dao.findDataTbDsjRawcheckC622ByPage((pageIndex - 1) * pageSize, pageIndex * pageSize, hot_coil_id, steelgrade, fin_grade, dbegin, dend, steelgroup);
            Integer count = tbDsjRawcheckC622Dao.findDataTbDsjRawcheckC622ByPageSize(hot_coil_id, steelgrade, fin_grade, dbegin, dend, steelgroup);
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 列表导出
     *
     * @param jsonObject
     * @param response
     */
    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");

        String hot_coil_id = null;//热卷号
        if (!StringUtils.isEmpty(condition.get("hot_coil_id"))) {
            hot_coil_id = condition.get("hot_coil_id").toString();
        }
        String steelgrade = null;//钢牌号
        if (!StringUtils.isEmpty(condition.get("steelgrade"))) {
            steelgrade = condition.get("steelgrade").toString();
        }
        String fin_grade = null;//等级
        if (!StringUtils.isEmpty(condition.get("fin_grade"))) {
            fin_grade = condition.get("fin_grade").toString();
        }


        String dbegin = null; //开始时间
        if (StringUtils.hasText(condition.getString("dbegin"))) {
            dbegin = condition.getString("dbegin");
        }

        String dend = null; //结束时间
        if (StringUtils.hasText(condition.getString("dend"))) {
            dend = condition.getString("dend");
        }

        List<Integer> steelgroup = new ArrayList<>(); // 钢种
        if (condition.getJSONArray("steelgroup") != null) {
            steelgroup = condition.getJSONArray("steelgroup");
        }

        // 创建表格数据集合
        List<TbDsjRawcheckC622> listExcel = new ArrayList<>();

        // 调用service查询出数据
        List<TbDsjRawcheckC622> data = null;
        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = tbDsjRawcheckC622Dao.findDataTbDsjRawcheckC622ByPage((i - 1) * pageSize, i * pageSize, hot_coil_id, steelgrade, fin_grade, dbegin, dend, steelgroup);
            for (TbDsjRawcheckC622 item : data) {
                listExcel.add(item);
            }
        }
        // 判断service查询出的数据集合data是否为空
        if (CollectionUtils.isEmpty(data)) {
            return;
        }
        List<ExportTbDsjRawcheckC622> list = new ArrayList<>();
        for (TbDsjRawcheckC622 item : listExcel) {
            ExportTbDsjRawcheckC622 exportTbDsjRawcheckC622 = new ExportTbDsjRawcheckC622();
            BeanUtils.copyProperties(item,exportTbDsjRawcheckC622);
            list.add(exportTbDsjRawcheckC622);
        }
        try {
            Map<String,Object> data1 = new HashMap();
            data1.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\2130原料库审核.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据主键查询单条记录
     *
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjRawcheckC622ByIndocno(String data) {
        try {
            JTbDsjRawcheckC622 jtbDsjRawcheckC622 = JSON.parseObject(data, JTbDsjRawcheckC622.class);
            Long indocno = jtbDsjRawcheckC622.getIndocno();

            TbDsjRawcheckC622 tbDsjRawcheckC622 = tbDsjRawcheckC622Dao.findDataTbDsjRawcheckC622ByIndocno(indocno);
            return ResultData.ResultDataSuccess(tbDsjRawcheckC622);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 查看记录
     *
     * @return list 对象集合返回
     */
    public List<TbDsjRawcheckC622> findDataTbDsjRawcheckC622() {
        List<TbDsjRawcheckC622> list = tbDsjRawcheckC622Dao.findDataTbDsjRawcheckC622();
        return list;
    }

    @Override
    public ResultData findDataByHotVolumeNos(JSONObject json) {
        JSONArray jsonArray = json.getJSONArray("volumeNos");
        List<String> volumeNos = jsonArray.toJavaList(String.class);
        if (CollectionUtils.isEmpty(volumeNos)) {
            return ResultData.ResultDataFaultSelf(null, "传入卷号为空");
        }

        Map<String, Map<String, List<Double>>> map = new HashMap<>();
        Query query1 = new Query(Criteria.where("volumeNo").in(volumeNos));
        List<QmsCurveDataMongo> list = mongoTemplate.find(query1, QmsCurveDataMongo.class);

        // qms_curve_data_mongo集合
        for (QmsCurveDataMongo qmsCurveDataMongo : list) {
            Map<String, List<Double>> mapValue = new HashMap<>();
//            Query query=new Query(Criteria.where("extid").is(no));
//            JDataSave621 jDataSave621 = mongoTemplate.findOne(query, JDataSave621.class);
            List<Double> list1 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdh_thk_cold");
            List<Double> list2 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdw_width_cold");
            List<Double> list3 = qmsCurveDataMongo.getOriginBolg().get("hsm_crown_c40");
            List<Double> list4 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_wedge40");
            List<Double> list5 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdt_fdt");
            List<Double> list6 = qmsCurveDataMongo.getOriginBolg().get("hsm_ctc_ct_temp");
            List<Double> list7 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_symmetric_flatness");
            List<Double> list8 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_asymmetric_flatness");

            mapValue.put("hsm_fdh_thk_cold", list1);
            mapValue.put("hsm_fdw_width_cold", list2);
            mapValue.put("hsm_crown_c40", list3);
            mapValue.put("hsm_flatness_wedge40", list4);
            mapValue.put("hsm_fdt_fdt", list5);
            mapValue.put("hsm_ctc_ct_temp", list6);
            mapValue.put("hsm_flatness_symmetric_flatness", list7);
            mapValue.put("hsm_flatness_asymmetric_flatness", list8);

            String volumeNo = qmsCurveDataMongo.getVolumeNo();
            if (StringUtils.hasText(volumeNo)) {
                map.put(volumeNo, mapValue);
            }
        }


        // datacollect622集合
        Query query = new Query(Criteria.where("extid").in(volumeNos));
        List<JDataSave622> jDataSave622List = mongoTemplate.find(query, JDataSave622.class);

        for (JDataSave622 jDataSave622 : jDataSave622List) {
            Map<String, List<Double>> mapValue = new HashMap<>();
            List<Double> list1 = jDataSave622.getRow().get("c622_tcm_exs5_gaugedev1_thk");
            List<Double> list2 = jDataSave622.getRow().get("c622_tcm_exs4_shape_width_act");
            List<Double> list3 = jDataSave622.getRow().get("c622_s5_speed");
            List<Double> list4 = jDataSave622.getRow().get("c622_tcm_shape_channel1");
            List<Double> list5 = jDataSave622.getRow().get("c622_tcm_shape_channel2");
            List<Double> list6 = jDataSave622.getRow().get("c622_tcm_shape_channel3");
            List<Double> list7 = jDataSave622.getRow().get("c622_tcm_shape_channel4");
            List<Double> list8 = jDataSave622.getRow().get("c622_tcm_shape_channel5");
            List<Double> list9 = jDataSave622.getRow().get("c622_tcm_shape_channel6");
            List<Double> list10 = jDataSave622.getRow().get("c622_tcm_shape_channel7");
            List<Double> list11 = jDataSave622.getRow().get("c622_tcm_shape_channel8");
            List<Double> list12 = jDataSave622.getRow().get("c622_tcm_shape_channel9");
            List<Double> list13 = jDataSave622.getRow().get("c622_tcm_shape_channel10");
            List<Double> list14 = jDataSave622.getRow().get("c622_tcm_shape_channel11");
            List<Double> list15 = jDataSave622.getRow().get("c622_tcm_shape_channel12");
            List<Double> list16 = jDataSave622.getRow().get("c622_tcm_shape_channel13");
            List<Double> list17 = jDataSave622.getRow().get("c622_tcm_shape_channel14");
            List<Double> list18 = jDataSave622.getRow().get("c622_tcm_shape_channel15");
            List<Double> list19 = jDataSave622.getRow().get("c622_tcm_shape_channel16");
            List<Double> list20 = jDataSave622.getRow().get("c622_tcm_shape_channel17");
            List<Double> list21 = jDataSave622.getRow().get("c622_tcm_shape_channel18");
            List<Double> list22 = jDataSave622.getRow().get("c622_tcm_shape_channel19");
            List<Double> list23 = jDataSave622.getRow().get("c622_tcm_shape_channel20");
            List<Double> list24 = jDataSave622.getRow().get("c622_tcm_shape_channel21");
            List<Double> list25 = jDataSave622.getRow().get("c622_tcm_shape_channel22");
            List<Double> list26 = jDataSave622.getRow().get("c622_tcm_shape_channel23");
            List<Double> list27 = jDataSave622.getRow().get("c622_tcm_shape_channel24");
            List<Double> list28 = jDataSave622.getRow().get("c622_tcm_shape_channel25");
            List<Double> list29 = jDataSave622.getRow().get("c622_tcm_shape_channel26");
            List<Double> list30 = jDataSave622.getRow().get("c622_tcm_shape_channel27");
            List<Double> list31 = jDataSave622.getRow().get("c622_tcm_shape_channel28");
            List<Double> list32 = jDataSave622.getRow().get("c622_tcm_shape_channel29");
            List<Double> list33 = jDataSave622.getRow().get("c622_tcm_shape_channel30");
            List<Double> list34 = jDataSave622.getRow().get("c622_tcm_shape_channel31");
            List<Double> list35 = jDataSave622.getRow().get("c622_tcm_shape_channel32");
            List<Double> list36 = jDataSave622.getRow().get("c622_tcm_shape_channel33");
            List<Double> list37 = jDataSave622.getRow().get("c622_tcm_shape_channel34");
            List<Double> list38 = jDataSave622.getRow().get("c622_tcm_shape_channel35");
            List<Double> list39 = jDataSave622.getRow().get("c622_tcm_shape_channel36");
            List<Double> list40 = jDataSave622.getRow().get("c622_tcm_shape_channel37");
            List<Double> list41 = jDataSave622.getRow().get("c622_tcm_shape_channel38");
            List<Double> list42 = jDataSave622.getRow().get("c622_tcm_shape_channel39");
            List<Double> list43 = jDataSave622.getRow().get("c622_tcm_shape_channel40");
            mapValue.put("c622_tcm_exs5_gaugedev1_thk", list1);
            mapValue.put("c622_tcm_exs4_shape_width_act", list2);
            mapValue.put("c622_s5_speed", list3);
            mapValue.put("c622_tcm_shape_channel1", list4);
            mapValue.put("c622_tcm_shape_channel2", list5);
            mapValue.put("c622_tcm_shape_channel3", list6);
            mapValue.put("c622_tcm_shape_channel4", list7);
            mapValue.put("c622_tcm_shape_channel5", list8);
            mapValue.put("c622_tcm_shape_channel6", list9);
            mapValue.put("c622_tcm_shape_channel7", list10);
            mapValue.put("c622_tcm_shape_channel8", list11);
            mapValue.put("c622_tcm_shape_channel9", list12);
            mapValue.put("c622_tcm_shape_channel10", list13);
            mapValue.put("c622_tcm_shape_channel11", list14);
            mapValue.put("c622_tcm_shape_channel12", list15);
            mapValue.put("c622_tcm_shape_channel13", list16);
            mapValue.put("c622_tcm_shape_channel14", list17);
            mapValue.put("c622_tcm_shape_channel15", list18);
            mapValue.put("c622_tcm_shape_channel16", list19);
            mapValue.put("c622_tcm_shape_channel17", list20);
            mapValue.put("c622_tcm_shape_channel18", list21);
            mapValue.put("c622_tcm_shape_channel19", list22);
            mapValue.put("c622_tcm_shape_channel20", list23);
            mapValue.put("c622_tcm_shape_channel21", list24);
            mapValue.put("c622_tcm_shape_channel22", list25);
            mapValue.put("c622_tcm_shape_channel23", list26);
            mapValue.put("c622_tcm_shape_channel24", list27);
            mapValue.put("c622_tcm_shape_channel25", list28);
            mapValue.put("c622_tcm_shape_channel26", list29);
            mapValue.put("c622_tcm_shape_channel27", list30);
            mapValue.put("c622_tcm_shape_channel28", list31);
            mapValue.put("c622_tcm_shape_channel29", list32);
            mapValue.put("c622_tcm_shape_channel30", list33);
            mapValue.put("c622_tcm_shape_channel31", list34);
            mapValue.put("c622_tcm_shape_channel32", list35);
            mapValue.put("c622_tcm_shape_channel33", list36);
            mapValue.put("c622_tcm_shape_channel34", list37);
            mapValue.put("c622_tcm_shape_channel35", list38);
            mapValue.put("c622_tcm_shape_channel36", list39);
            mapValue.put("c622_tcm_shape_channel37", list40);
            mapValue.put("c622_tcm_shape_channel38", list41);
            mapValue.put("c622_tcm_shape_channel39", list42);
            mapValue.put("c622_tcm_shape_channel40", list43);

            String entid = jDataSave622.getEntid();
            if (StringUtils.hasText(entid)) {
                map.put(entid, mapValue);
            }
        }

        if (!CollectionUtils.isEmpty(map)) {
            return ResultData.ResultDataSuccess(map);
        }
        return ResultData.ResultDataFaultSelf(null, "查询数据为空");
    }

}
